package main

import (
	"context"
	"fmt"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/client"
	"github.com/docker/go-connections/nat"
	"time"
)

func main() {
	c, err := client.NewClientWithOpts()

	if err != nil {
		panic(err)
	}

	ctx := context.Background()

	resp, err := c.ContainerCreate(
		ctx,
		&container.Config{
			Image:        "mongo",
			ExposedPorts: nat.PortSet{"27017/tcp": {}},
		},
		&container.HostConfig{
			PortBindings: nat.PortMap{
				"27017/tcp": []nat.PortBinding{
					{
						HostIP:   "127.0.0.1",
						HostPort: "0",
					},
				},
			},
		}, nil, nil, "",
	)

	if err != nil {
		panic(err)
	}

	err = c.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
	if err != nil {
		panic(err)
	}

	inspect, err := c.ContainerInspect(ctx, resp.ID)
	if err != nil {
		panic(err)
	}

	port := inspect.NetworkSettings.Ports["27017/tcp"][0]

	fmt.Printf("listening at %v\n", port)

	fmt.Println("container started")
	time.Sleep(5 * time.Second)
	fmt.Println("container kill")

	err = c.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{
		Force: true,
	})
	if err != nil {
		panic(err)
	}
}
